package in.muton.edushare.pages.assignment;

import java.util.Date;

import in.muton.edushare.wicket.models.CayenneList;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.AssignmentsDAO;
import com.edushare.common.util.DateTimeUtil;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Assignments;
import com.edushare.common.vo.FilesUploaded;

public class Detail extends AssignmentBase {

	public CayenneList<FilesUploaded> helpFiles;
	
	public Detail(PageParameters params) {
		super(params);
		helpFiles = new CayenneList<FilesUploaded>
						(
								new AssignmentsDAO(getSessionORMInterface())
								.getHelpDirectory(model.getObject())
								.getFilesUploaded()
						);
		loadPage();
	}

	private void loadPage() {
		
		add(new Label("submStatus", Assignments.getStatusAsString(model.getObject().getStatus()).toUpperCase()));
		add(new Label("sdate", DateTimeUtil.formatDate(model.getObject().getStartDate())));
		add(new Label("edate", DateTimeUtil.formatDate(model.getObject().getEndDate())));
		if(model.getObject().getAllowLate()== Assignments.ASSIGNMENT_ALLOW_LATE_SUBMIT){
			add(new Label("ldate", "till " + DateTimeUtil.formatDate(model.getObject().getLateSubmDate())));
		}else{
			add(new Label("ldate", ""){
				private static final long serialVersionUID = 7950674189728661036L;

				@Override
				public boolean isVisible() {
					return false;
				}
			});
		}
		add(new MultiLineLabel("assnDetail", new PropertyModel<String>(model, "assignmentDesc")).setEscapeModelStrings(false));
		if(helpFiles.getObject()==null || helpFiles.getObject().size()<1){
			add(new FileInfoFrag("helpFilesDiv", this, new Model<String>("No assignment help file available.")));
		}else{
			add(new AttachedFileFrag("helpFilesDiv", this, helpFiles));
		}
		
		if(new AssignmentsDAO(getSessionORMInterface()).isSolutionFilesVisible(getModel().getObject())){
			CayenneList<FilesUploaded> solnFiles
											= new CayenneList<FilesUploaded>
												(
														new AssignmentsDAO(getSessionORMInterface())
														.getSolutionDirectory(model.getObject())
														.getFilesUploaded()
												);
			if(solnFiles.getObject()==null || solnFiles.getObject().size()<1){
				add(new FileInfoFrag("solnFilesDiv", this, new Model<String>("No solution file was uploaded.")));
			}else{
				add(new AttachedFileFrag("solnFilesDiv", this, solnFiles));
			}
		}else{
			Date showSolnDate = 
				(getModel().getObject().getAllowLate()==Assignments.ASSIGNMENT_ALLOW_LATE_SUBMIT)?
						getModel().getObject().getLateSubmDate():getModel().getObject().getEndDate();
			add(new FileInfoFrag("solnFilesDiv", this, new Model<String>("Solution files will be available after " + 
																			DateTimeUtil.formatTimestamp(showSolnDate) + 
																			". (if available)")));
		}
	}

	@Override
	protected String getActivePage() {
		return "assnDetails";
	}

	@Override
	public String getPageTitle() {
		return "Assignment Details | "+ getModel().getObject().getAssignmentTitle()+" | " + ApplicationConstants.APPLICATION_NAME;
	}

	public class AttachedFileFrag extends Fragment{

		private static final long serialVersionUID = -2775547777821133697L;

		public AttachedFileFrag(String id, MarkupContainer container, CayenneList<FilesUploaded> model) {
			super(id, "attachedFileFrag", container, model);
			add(
					new ListView<IModel<FilesUploaded>>("helpFiles",model){

						private static final long serialVersionUID = 7315597272001945517L;

						@Override
						protected void populateItem(ListItem<IModel<FilesUploaded>> item) {
							item.add(new ExternalLink(
											"hfileLink", 
											new PropertyModel<String>(item.getModelObject(), "fileDownloadUrl"), 
											new PropertyModel<String>(item.getModelObject(),"fileOrigName")
									));
						}
						
					}
				);
		}
		
	}
	
	public class FileInfoFrag extends Fragment{

		private static final long serialVersionUID = 8683573986309126573L;

		public FileInfoFrag(String id,MarkupContainer container, IModel<String> warningModel) {
			super(id, "fileInfoFrag", container, warningModel);
			add(new Label("infoDiv", warningModel));
		}
		
	}
	
	
}
